# -*- coding: utf-8 -*-
##############################################################################
#
#    baidu Solution(Odoo Expert)
#    Copyright (C) 2015 baidu Soluation (<http://baidu.com/>)
#
##############################################################################

import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
from openerp.tools.translate import _
from openerp.exceptions import except_orm

from odoo import models, fields, api

class inventory_wizard_line(models.Model):

    _name= 'inventory.age.wizard.line'

    period_length= fields.Integer(u'分析周期 (天)', default=30)
    wizard_id= fields.Many2one('inventory.age.wizard', u'分析表')
    name= fields.Many2one('product.product', u'产品')
    lot_id= fields.Many2one('stock.production.lot', u'序列号')
    product_category_id = fields.Many2one('product.category', u'产品类型')
    warehouse_id= fields.Many2one('stock.warehouse', u'仓库')
    location_id= fields.Many2one('stock.location', u'库位')
    company_id= fields.Many2one('res.company', u'公司',default=lambda self: self.env.user.company_id)
    date_from= fields.Date(u'节点时间', default=lambda *a: time.strftime('%Y-%m-%d'))
    date_now=fields.Float(u'当前可用总数')
    date_8=fields.Float(u'(0-1)周期')
    date_7=fields.Float(u'(1-2)周期')
    date_6=fields.Float(u'(2-3)周期')
    date_5=fields.Float(u'(3-4)周期')
    date_4=fields.Float(u'(4-5)周期')
    date_3=fields.Float(u'(5-6)周期')
    date_2=fields.Float(u'(6-7)周期')
    date_1=fields.Float(u'(7-8)周期')
    date_0=fields.Float(u'(8)周期以前')


class inventory_wizard(models.Model):

    _name= 'inventory.age.wizard'
    _description = 'Stock Aging Report'
    _order = 'date_from desc'

    period_length= fields.Integer(u'分析周期 (天)', default=30)
    product_id= fields.Many2one('product.product', u'产品')
    product_category_id = fields.Many2one('product.category', u'产品类型')
    warehouse_id= fields.Many2one('stock.warehouse', u'仓库')
    location_id= fields.Many2one('stock.location', u'库位')
    company_id= fields.Many2one('res.company', u'公司',default=lambda self: self.env.user.company_id)
    date_from= fields.Date(u'节点时间', default=lambda *a: time.strftime('%Y-%m-%d'))
    line_count= fields.Integer(u'明细条目',compute='_compute_line_count')
    line_ids= fields.One2many('inventory.age.wizard.line','wizard_id',u'报表明细')

    @api.depends('line_ids')
    def _compute_line_count(self):
        for order in self:
            i=0
            for line in order.line_ids:
                i+=1
            order.line_count = i

    @api.model
    def default_get(self, fields):
        res = super(inventory_wizard, self).default_get(fields)
        warehouse = None
        if 'warehouse_id' not in res and res.get('company_id'):
            warehouse = self.env['stock.warehouse'].search([('company_id', '=', res['company_id'])], limit=1)
        if warehouse:
            res['warehouse_id'] = warehouse.id
            res['location_id'] = warehouse.lot_stock_id.id
        return res


    def _print_report(self, data):
        res = {}

        period_length = data['form']['period_length']
        if period_length <= 0:
            raise except_orm(_('User Error!'), _(u'分析周期必须大于0天.'))
        if not data['form']['date_from']:
            raise except_orm(_('User Error!'), _(u'必须设置一个分析节点时间.'))

        start = datetime.strptime(data['form']['date_from'], "%Y-%m-%d")
        # for i in range(5)[::-1]:
        for i in range(9)[::-1]:
            stop = start - relativedelta(days=period_length)
            res[str(i)] = {
                # 'name'  : (i != 0 and (str((5 - (i + 1)) * period_length) + '-' + str((5 - i) * period_length)) or ('+' + str(4 * period_length))),
                'name'  : (i != 0 and (str((9 - (i + 1)) * period_length) + '-' + str((9 - i) * period_length)) or ('+' + str(8 * period_length))),
                'stop'  : start.strftime('%Y-%m-%d'),
                'start' : (i != 0 and stop.strftime('%Y-%m-%d') or False),
            }
            start = stop - relativedelta(days=1)
        data['form'].update(res)
        return self.get_lines(data.get('form'))
        # return self.env.get('report').get_action([], 'inventory_age_report.report_stockageing', data=data)


    @api.multi
    def check_report(self):
        data = {}
        data['ids'] = self._context.get('active_ids', [])
        data['model'] = self._context.get('active_model', 'ir.ui.menu')
        for record in self:
            data['form']= record.read(['period_length', 'product_id', 'product_category_id', 'warehouse_id', 'location_id', 'company_id', 'date_from'])[0]
            res= record._print_report(data)
            if res:
                # 创建tree视图结果
                for line in res:
                    self.env['inventory.age.wizard.line'].create({
                        'wizard_id' :record.id,
                        'period_length' :data['form']['period_length'],
                        'name' :line.get('product_id') and line.get('product_id').id or False,
                        'lot_id': line.get('lot_id') and line.get('lot_id') or False,
                        'product_category_id' :data['form']['product_category_id'][0],
                        'warehouse_id' :data['form']['warehouse_id'][0],
                        'location_id' :line.get('location_id') and line.get('location_id') or False,
                        'company_id' :data['form']['company_id'][0],
                        'date_from' :data['form']['date_from'],
                        'date_now' :line.get('onhand_qty',False),
                        'date_8' :line.get('8',False),
                        'date_7' :line.get('7',False),
                        'date_6' :line.get('6',False),
                        'date_5' :line.get('5',False),
                        'date_4' :line.get('4',False),
                        'date_3' :line.get('3',False),
                        'date_2' :line.get('2',False),
                        'date_1' :line.get('1',False),
                        'date_0' :line.get('0',False),
                    })
        return self.action_view_line()

    @api.multi
    def action_view_line(self):

        action = self.env.ref('inventory_age_report_tree.action_age_report_line_tree')
        result = action.read()[0]
        result.pop('id', None)
        result['context'] = {}
        line_ids = sum([order.line_ids.ids for order in self], [])
        result['domain'] = "[('id','in',[" + ','.join(map(str, line_ids)) + "])]"
        return result


    @api.model
    def get_lines(self, form):
        res = []

        quant_obj = self.env.get('stock.quant')
        product_category_id = form['product_category_id'][0]
        product_obj = self.env.get('product.product')
        products = product_obj.search([('categ_id', '=', product_category_id)])
        product_ids = products._ids
        if form.get('product_id', False) and form['product_id'][0] in product_ids:
            wizard_product_id = form['product_id'][0]
            product_ids = [wizard_product_id]
        # 产品循环
        for product in product_obj.browse(product_ids):
            product_dict = {
                'pname': product.name,
                'product_id': product
            }
            # 循环库位，如果没有选择库位，就搜当前公司所有内部库位
            location_ids=[]
            if form['location_id']:
                location_ids.append(form['location_id'][0])
            else:
                location_ids=self.env['stock.location'].search([('company_id', '=', form['company_id'][0]),('usage', '=', 'internal')]).ids
            # 开始库位循环
            for location_id in location_ids:
                date_from = form['date_from']
                # warehouse = form['warehouse_id'][0]
                ctx = self._context.copy()
                ctx.update({
                    'location': location_id,
                    'from_date': date_from,
                    'to_date': date_from
                })
                product_qty = product._product_available(False, False)
                qty_list = product_qty.get(product.id)
                product_dict.update({
                    'onhand_qty' : qty_list['qty_available'],
                })
                # 找序列号，然后根据序列号循环
                lot_ids = []
                slack_domain = [('location_id', '=', location_id),
                                ('product_id', '=', product.id)]
                quant_search = quant_obj.search(slack_domain)
                for quant_lot in quant_search:
                    if quant_lot.lot_id.id not in lot_ids:
                        lot_ids.append(quant_lot.lot_id.id)
                if lot_ids:
                    for lot in lot_ids:
                        # if lot==False:
                        #     continue
                        product_dict = {
                            'pname': product.name,
                            'product_id': product,
                            'onhand_qty': qty_list['qty_available'],
                            'lot_id': lot,
                            'location_id': location_id,
                        }
                        # for data in range(0, 6):
                        for data in range(0, 10):
                            total_qty = 0
                            if form.get(str(data)):
                                start_date = form.get(str(data)).get('start')
                                stop_date = form.get(str(data)).get('stop')
                                if not start_date:
                                    domain = [('in_date', '<=', stop_date), ('location_id', '=', location_id),
                                              ('product_id', '=', product.id),('lot_id', '=', lot)]
                                else:
                                    domain = [('in_date', '<=', stop_date), ('in_date', '>=', start_date),
                                              ('location_id', '=', location_id), ('product_id', '=', product.id),('lot_id', '=', lot)]

                                for quant in quant_obj.search(domain):
                                    total_qty += quant.qty
                                product_dict[str(data)] = total_qty
                        res.append(product_dict)

        return res

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
